/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.LayerTree = function(config) {

	Ext.apply(this, config, {
			autoScroll: true
		,	animate: true
		,	enableDD: true
		,	border: false
		,	id: 'layertree'
		,	plugins: new Ext.ux.DataTip({
				tpl: new Ext.XTemplate(
					'<div>'+
						'<tpl if="mType != 0">'+
							'Type: {mType} '+
						'</tpl>'+
						'<tpl if="description != 0">'+
							'<br> Description: {description}</div>'+
						'</tpl>'+	
					'</div>'							
				)
			})
		,	tbar: [{
					text: this.txtNewFolder
				,	scope: this	
				,	handler: this.addNewFolder
				,	iconCls: 'icon-addlayer'
			},{
					text: this.txtEditFolder
				,	scope: this
				,	handler: this.editLayerName
				,	iconCls: 'icon-editlayer'	
			},{
					text: this.txtDeleteFolder
				,	scope: this	
				,	handler: this.deleteLayerName
				,	iconCls: 'icon-deletelayer'	
			}]
		,	bbar: [{
					text: this.txtSaveTreeConfig
				,	ref: '../savetreeconfig'	
				,	scope: this	
				,	iconCls: 'icon-savelayer'	
			},{
					text: this.txtOverwriteConfig
				,	ref: '../overwriteconfig'	
				,	scope: this	
				,	iconCls: 'icon-overwritelayer'	
			}]
	});

	GBIF.GeoSpatial.LayerTree.superclass.constructor.call(this, config);

	this.on('contextmenu', this.showMenu, this);
}

Ext.extend(GBIF.GeoSpatial.LayerTree, GBIF.GeoSpatial.AvailableLayersTree, {	
		
		sendCapabilitiesRequest: function(node){
			var layer = node.attributes.layer;
			var url = layer.url;
			var version = layer.version;
			var service = node.attributes.mType;
			var url = url + "?request=getCapabilities&ver=" + version + "&service=" + service;
			window.open(url, '_blank');
		}
	
	,	showMenu: function(node) {
			var menu = new Ext.menu.Menu();
			if (node.attributes.leaf) {
				menu.add(new Ext.menu.Item({
						text: "View GetCapabilities" //this.txtDeleteFolder
					,	handler: this.sendCapabilitiesRequest.createDelegate(this, [node])
				}));
				menu.add('-');
			}
			menu.add(new Ext.menu.Item({
					text: this.txtAddFolder
				,	handler: this.addNewFolder.createDelegate(this, [node]) //character_add_attribute.createDelegate(this, [node])
			}));
			menu.add(new Ext.menu.Item({
					text: this.txtEditFolder
				,	handler: this.editLayerName.createDelegate(this, [node])
			}));
			menu.add('-');
			menu.add(new Ext.menu.Item({
					text: this.txtDeleteFolder
				,	handler: this.deleteLayerName.createDelegate(this, [node])
			}));
			menu.show(node.ui.getAnchor());		
		}

	,	addNewFolder: function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			
			if(nodetype){
				node.select()
			}
			var treeeditor = new Ext.tree.TreeEditor(this, { }, {
					allowBlank: false
				,	blankText: 'A name is required'
				,	selectOnFocus: true
			});
			
			var root = this.getRootNode();
			if(this.getSelectionModel().selNode){
				root = this.getSelectionModel().selNode;
			}
			
			if(root.attributes.type == 'fld' || !Ext.isDefined(root.attributes.type) && Ext.isEmpty(root.attributes.mType) ){
			} else {
				var nodeType = root.attributes.type;
				nodeType =(typeof nodeType == 'undefined') ? root.attributes.mType : nodeType;
				GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtGBIFNodeError + nodeType );
				return;		
			}
			
			var node = root.appendChild(new Ext.tree.TreeNode({
					text:'New Folder'
				,	allowDrag:true
				,	leaf:false
				,	expanded:true
				,	type:'fld'
				,	mType:''
				,	description:''
				,	layer:''
			}));
			 
//			node.attributes.id =  node.text;
			 
			this.getSelectionModel().select(node);
			setTimeout(function(){
				treeeditor.editNode = node;
				treeeditor.startEdit(node.ui.textNode);
			}, 10);
		}
		
	,	editLayerName:function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			if (nodetype) {
				node.select();
			}else{
				if (this.getSelectionModel().selNode) {
					node = this.getSelectionModel().selNode;
				} else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtNodeError);
					return;
				}	
			}	
			
			if (node.isRoot) {
					GBIF.GeoSpatial.Notice.msg(this.txtError,this.txtRootNodeEditError);
					return;
				}
			
			var treeeditor = new Ext.tree.TreeEditor(this, {}, {
					allowBlank: false
				,	blankText: 'A name is required'
				,	selectOnFocus: true
			});
			setTimeout(function(){
				treeeditor.editNode = node;
				treeeditor.startEdit(node.ui.textNode);
			}, 10);

			node.on('textchange',function(){
				node.attributes.id=node.attributes.text
			}, this);
		}
	
	,	deleteLayerName:function(node){
			var	nodetype = (node.type == 'button') ? false : true;
			if(nodetype){
				node.select();
			} else {
				if (this.getSelectionModel().selNode) {
					node = this.getSelectionModel().selNode;
				} else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtNodeError);
					return;
				}
			}
			if (node.isRoot) {
				GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtRootNodeError);
			}else {
				Ext.MessageBox.confirm(this.txtConfirm, this.txtConfirmMsg1 + node.text + this.txtConfirmMsg2, this.deleteNode);
			}	
		}
		
	,	deleteNode: function(btn, text){
			if (btn == 'yes') {
				var lt = Ext.getCmp('layertree');
				var node = lt.getSelectionModel().selNode;
				node.destroy();
			}
		}

});